{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Find First Palindromic String in the Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #two-pointers #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #双指针 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: firstPalindrome"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #找出数组中的第一个回文字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串数组 <code>words</code> ，找出并返回数组中的 <strong>第一个回文字符串</strong> 。如果不存在满足要求的字符串，返回一个 <strong>空字符串</strong><em> </em><code>\"\"</code> 。</p>\n",
    "\n",
    "<p><strong>回文字符串</strong> 的定义为：如果一个字符串正着读和反着读一样，那么该字符串就是一个 <strong>回文字符串</strong> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>words = [\"abc\",\"car\",\"ada\",\"racecar\",\"cool\"]\n",
    "<strong>输出：</strong>\"ada\"\n",
    "<strong>解释：</strong>第一个回文字符串是 \"ada\" 。\n",
    "注意，\"racecar\" 也是回文字符串，但它不是第一个。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>words = [\"notapalindrome\",\"racecar\"]\n",
    "<strong>输出：</strong>\"racecar\"\n",
    "<strong>解释：</strong>第一个也是唯一一个回文字符串是 \"racecar\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>words = [\"def\",\"ghi\"]\n",
    "<strong>输出：</strong>\"\"\n",
    "<strong>解释：</strong>不存在回文字符串，所以返回一个空字符串。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= words.length &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= words[i].length &lt;= 100</code></li>\n",
    "\t<li><code>words[i]</code> 仅由小写英文字母组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [find-first-palindromic-string-in-the-array](https://leetcode.cn/problems/find-first-palindromic-string-in-the-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [find-first-palindromic-string-in-the-array](https://leetcode.cn/problems/find-first-palindromic-string-in-the-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"abc\",\"car\",\"ada\",\"racecar\",\"cool\"]', '[\"notapalindrome\",\"racecar\"]', '[\"def\",\"ghi\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for w in words:\n",
    "            if w==w[::-1]:return w\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for ww in words:\n",
    "            if ww==ww[::-1]:\n",
    "                return ww\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for i in words:\n",
    "            if i == i[::-1]:\n",
    "                return i\n",
    "\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        return  next((word for word in words if word == word[::-1] ),\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        def isPalindrome(word:str)-> bool:\n",
    "            n = len(word)\n",
    "            l,r = 0,n-1\n",
    "            while l<r:\n",
    "                if word[l] != word[r]:\n",
    "                    return False\n",
    "                l+=1\n",
    "                r-=1\n",
    "            return True\n",
    "        \n",
    "        for word in words:\n",
    "            if isPalindrome(word):\n",
    "                return word\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for word in words:\n",
    "            for i in range(len(word) // 2):\n",
    "                if word[i] != word[-i-1]:\n",
    "                    break\n",
    "            else:\n",
    "                return word\n",
    "        return \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        def check(s):\n",
    "            p, q = 0, len(s)-1\n",
    "            while p <= q:\n",
    "                if s[p] != s[q]:\n",
    "                    return False\n",
    "                p += 1\n",
    "                q -= 1\n",
    "            return True\n",
    "        for word in words:\n",
    "            if check(word):\n",
    "                return word\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for w in words:\n",
    "            if w == w[::-1]: return w\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        return next((i for i in words if i==i[::-1]),'')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        def isPrime(words: str) -> bool:\n",
    "            i, j = 0, len(words) - 1\n",
    "            while i < j:\n",
    "                if words[i] == words[j]:\n",
    "                    i += 1\n",
    "                    j -=1 \n",
    "                else:\n",
    "                    return False\n",
    "            return True\n",
    "        for word in words:\n",
    "            if isPrime(word):\n",
    "                return word\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for w in words:\n",
    "            s=True\n",
    "            for i in range(len(w)//2):\n",
    "                if w[i]!=w[-i-1]:\n",
    "                    s=False\n",
    "                    break\n",
    "            if s:\n",
    "                return w\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for i in words:\n",
    "            if i==i[::-1]:\n",
    "                return i \n",
    "                break\n",
    "        else:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for word in words:\n",
    "            if word == word[::-1]:\n",
    "                return word\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for word in words:\n",
    "            if word == word[::-1]:\n",
    "                return word\n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for word in words:\n",
    "            if word == word[::-1]:\n",
    "                return word\n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for s in words:\n",
    "            if s == s[::-1]:\n",
    "                return s\n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for word in words:\n",
    "            if word == word[::-1]:\n",
    "                return word\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for i in words:\n",
    "            if i == i[::-1]:\n",
    "                return i\n",
    "\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        def g(s):\n",
    "            n=len(s)\n",
    "            l=0\n",
    "            r=n-1 \n",
    "            while l<r:\n",
    "                if s[l]!=s[r]:\n",
    "                    return False\n",
    "                l+=1\n",
    "                r-=1\n",
    "            return True\n",
    "        for x in words:\n",
    "            if g(x):\n",
    "                return x\n",
    "        return \"\"    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for i in words:\n",
    "            if i==i[::-1]:\n",
    "                return i\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for word in words:\n",
    "            word_lst = list(word)\n",
    "            if word_lst == word_lst[::-1]:\n",
    "                return \"\".join(word_lst)\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isOrNot(self, s):\n",
    "        for i in range(int(len(s)/2)):\n",
    "            if s[i]!=s[len(s)-1-i]:\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for word in words:\n",
    "            if self.isOrNot(word) is True:\n",
    "                return word\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        count=0\n",
    "        for word in words:\n",
    "            if word==word[::-1]:\n",
    "                return word\n",
    "        if count==0:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for w in words:\n",
    "            if w == w[::-1]: return w\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "\n",
    "        if len(words) == 0:\n",
    "            return \n",
    "        for word in words:\n",
    "            if word == word[::-1]:\n",
    "                return word\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for x in words:\n",
    "            if x == x[::-1]:\n",
    "                return x\n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for i in words:\n",
    "            if i == i[::-1]:\n",
    "                return i\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for w in words:\n",
    "            if w == w[::-1]:\n",
    "                return w\n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        return next((w for w in words if w == w[::-1]), \"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "\n",
    "        def check(s:str):\n",
    "            return s==s[::-1]\n",
    "            \n",
    "        for i in words:\n",
    "            if check(i):\n",
    "                return i\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for i in words:\n",
    "            if i==i[::-1]:\n",
    "                return i\n",
    "        return \"\"        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        s = False\n",
    "        for w in words:\n",
    "            s = True\n",
    "            l = len(w)\n",
    "            if l == 1:\n",
    "                return w\n",
    "            for i in range(int(l / 2)):\n",
    "                if w[i] != w[l - i - 1]:\n",
    "                    s = False\n",
    "                    break\n",
    "            if s:\n",
    "                return w\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        return next((word for word in words if word == word[::-1]), \"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        def isPalindrome(word):\n",
    "            n = len(word)\n",
    "            l, r = 0, n-1\n",
    "            while l < r:\n",
    "                if word[l] != word[r]:\n",
    "                    return False\n",
    "                l += 1\n",
    "                r -= 1\n",
    "            return True\n",
    "        for word in words:\n",
    "            if isPalindrome(word):\n",
    "                return word\n",
    "        return ''\n",
    "                    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # def firstPalindrome(self, words: List[str]) -> str:\n",
    "    #     for word in words:\n",
    "    #         if word==word[::-1]:\n",
    "    #             return word\n",
    "    #     return \"\"\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        def isPalindrom(word):\n",
    "            left,right=0,len(word)-1\n",
    "            while left<right:\n",
    "                if word[left]==word[right]:\n",
    "                    left+=1\n",
    "                    right-=1\n",
    "                else:\n",
    "                    return False\n",
    "            return True\n",
    "\n",
    "        for word in words:\n",
    "            if isPalindrom(word):\n",
    "                return word\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        return next((s for s in words if s==s[::-1]), \"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        def subkk(s:str) -> bool:\n",
    "            left = 0\n",
    "            right = len(s) - 1\n",
    "            while(left < right):\n",
    "                if s[left] != s[right]:\n",
    "                    return False\n",
    "                left += 1\n",
    "                right -= 1\n",
    "            return True\n",
    "        for v in words:\n",
    "            if subkk(v) == True:\n",
    "                return v\n",
    "        return \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for word in words:\n",
    "            for i in range(len(word) // 2):\n",
    "                if word[i] != word[-i-1]:\n",
    "                    break\n",
    "            else:\n",
    "                return word\n",
    "        return \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for i in words:\n",
    "            if i == i[::-1]:\n",
    "                return i\n",
    "            \n",
    "        else:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for i in words:\n",
    "            if i==i[::-1]:\n",
    "                return i\n",
    "        return \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        ans = \"\"\n",
    "        def check(word: str) -> bool:\n",
    "            left, right = 0, len(word) - 1\n",
    "            while left < right:\n",
    "                if word[left] != word[right]:\n",
    "                    return False\n",
    "                left += 1\n",
    "                right -= 1\n",
    "\n",
    "            return True\n",
    "        \n",
    "        for i in words:\n",
    "            if check(i):\n",
    "                return i\n",
    "        \n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        return next((word for word in words if word == word[::-1]), \"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for i in words:\n",
    "            if i==i[::-1]:\n",
    "                return i \n",
    "                break\n",
    "        else:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        def is_huiwen(s):\n",
    "            length = len(s)\n",
    "            for i in range(length//2):\n",
    "                if s[i] != s[length-i-1]:\n",
    "                    return False\n",
    "            return True\n",
    "        \n",
    "        ans = \"\"\n",
    "        for word in words:\n",
    "            if is_huiwen(word):\n",
    "                return word\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        i=j=0\n",
    "        while i<len(words):\n",
    "            if len(words[i])==1:\n",
    "                return words[i]\n",
    "                break\n",
    "            while words[i][j]==words[i][-j-1]:\n",
    "                j+=1\n",
    "                if j==len(words[i])//2:\n",
    "                    return words[i]\n",
    "                    break\n",
    "            i+=1\n",
    "            j=0\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for i in words:\n",
    "            if i == i[::-1]:\n",
    "                return i\n",
    "        return \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for w in words:\n",
    "            if w ==w[::-1]:return w\n",
    "        return \"\"\n",
    "           "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for w in words:\n",
    "            if w == w[::-1]: \n",
    "                return w\n",
    "        return \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for w in words:\n",
    "            s=True\n",
    "            for i in range(len(w)):\n",
    "                if w[i]!=w[-i-1]:\n",
    "                    s=False\n",
    "                    break\n",
    "            if s:\n",
    "                return w\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for i in words:\n",
    "            if i==i[::-1]:\n",
    "                return i \n",
    "                break\n",
    "        else:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for i in words:\n",
    "            if i==i[::-1]:\n",
    "                return i \n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        s = \"\"\n",
    "        for word in words:\n",
    "            if word == word[::-1]:\n",
    "                s = word\n",
    "                return s \n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for word in words:\n",
    "            if word == word[::-1]:\n",
    "                return word\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for x in words:\n",
    "            if x == x[::-1]:\n",
    "                return x\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for word in words:\n",
    "            if word == word[::-1]:\n",
    "                return word\n",
    "        return ''\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for w in words:\n",
    "            if w == w[::-1]:\n",
    "                return w\n",
    "            \n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for i in words:\n",
    "            if i==i[::-1]:\n",
    "                return i\n",
    "\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for i in words:\n",
    "            if i == i[::-1]:\n",
    "                return i\n",
    "        else:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        \n",
    "        for i in words:\n",
    "            if i == i[::-1]:\n",
    "                return i\n",
    "        \n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        def checkpa(word:str)->bool:\n",
    "            if len(word)==1:return True\n",
    "            i=0\n",
    "            j=len(word)-1\n",
    "            while i<j:\n",
    "                if word[i]!=word[j]:return False\n",
    "                i+=1\n",
    "                j-=1\n",
    "            return True\n",
    "\n",
    "        x=''\n",
    "        for w in words:\n",
    "            if checkpa(w): return w\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for word in words:\n",
    "            if word == word[::-1]:\n",
    "                return word\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for i in words:\n",
    "            for j in range(len(i) // 2):\n",
    "                if i[j] != i[-1 + (-j)]:\n",
    "                    break\n",
    "            else:\n",
    "                return i\n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for word in words:\n",
    "            half_length = int(len(word) / 2)\n",
    "            if word[:half_length + 1] == word[len(word) - half_length - 1:][::-1]:\n",
    "                return word\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for word in words:\n",
    "            if word==word[::-1]:\n",
    "                return word\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for word in words:\n",
    "            left , right = 0 , len(word) - 1\n",
    "            flag = 1\n",
    "            while left < right:\n",
    "                if word[left] != word[right]:\n",
    "                    flag = 0\n",
    "                    break\n",
    "                left += 1\n",
    "                right -= 1\n",
    "            if flag == 1:\n",
    "                return word\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        def dis(x:str)->bool:\n",
    "            return x==x[::-1]\n",
    "        for x in words:\n",
    "            if dis(x):\n",
    "                return x\n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        def isPalindrome(s: str) -> bool:\n",
    "            left,right = 0,len(s)-1\n",
    "            while left<right:\n",
    "                while left<right and not s[left].isalnum():#不是空格或符号\n",
    "                    left += 1\n",
    "                while left<right and not s[right].isalnum():#不是空格或符号\n",
    "                    right -= 1\n",
    "                if left<right:\n",
    "                    if s[left].lower() !=s[right].lower():\n",
    "                        return False\n",
    "                    left,right = left+1,right-1\n",
    "            return True\n",
    "        for i in range(len(words)):\n",
    "            if isPalindrome(words[i]):return words[i]\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for x in words:\n",
    "            left = 0\n",
    "            right = len(x) - 1\n",
    "            while left < right and x[left] == x[right]:\n",
    "                left += 1\n",
    "                right -= 1\n",
    "            if x[left] == x[right]:\n",
    "                return x\n",
    "            else:\n",
    "                continue\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for word in words:\n",
    "            if word[::-1] == word:\n",
    "                return word\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for word in words:\n",
    "            if word==word[::-1]:\n",
    "                return word\n",
    "        return \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for word in words:\n",
    "            if word == word[::-1]:\n",
    "                return word\n",
    "        else:\n",
    "            return \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        return next((s for s in words if s == s[::-1]), '')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        for i in words:\n",
    "            if i == i[::-1]:\n",
    "                return i\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "    # def firstPalindrome(self, words: List[str]) -> str:\n",
    "    #     for word in words:\n",
    "    #         if word==word[::-1]:\n",
    "    #             return word\n",
    "    #     return \"\"\n",
    "    # def firstPalindrome(self, words: List[str]) -> str:\n",
    "    #     def isPalindrom(word):\n",
    "    #         left,right=0,len(word)-1\n",
    "    #         while left<right:\n",
    "    #             if word[left]==word[right]:\n",
    "    #                 left+=1\n",
    "    #                 right-=1\n",
    "    #             else:\n",
    "    #                 return False\n",
    "    #         return True\n",
    "\n",
    "    #     for word in words:\n",
    "    #         if isPalindrom(word):\n",
    "    #             return word\n",
    "    #     return \"\"\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "        def ispalind(s):\n",
    "            left=0\n",
    "            right=len(s)-1\n",
    "            while left<right:\n",
    "                if s[left]!=s[right]:\n",
    "                    return False\n",
    "                else:\n",
    "                    left+=1\n",
    "                    right-=1\n",
    "            return True\n",
    "        for word in words:\n",
    "            if ispalind(word):\n",
    "                return word\n",
    "        return \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def firstPalindrome(self, words: List[str]) -> str:\n",
    "\n",
    "        def isPalindrome(word):\n",
    "            left, right = 0, len(word) - 1\n",
    "            while left < right:\n",
    "                if word[left] != word[right]:\n",
    "                    return False\n",
    "\n",
    "                left += 1\n",
    "                right -= 1\n",
    "\n",
    "            return True\n",
    "\n",
    "        for word in words:\n",
    "            if isPalindrome(word):\n",
    "                return word\n",
    "        return \"\""
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
